package com.qianfeng.controller;

import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Role;
import com.qianfeng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@ResponseBody
@CrossOrigin
@RequestMapping(value = "/role")
@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/addRole")
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        Result result = new Result();
        return result;
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/deleteRole")
    public Result deleteRole(Integer roleId){
        roleService.deleteRole(roleId);
        Result result = new Result();
        return result;
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @RequestMapping(value = "/updateRole")
    public Result updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/getRolePage")
    public Result getRolePage(Integer pageNo,Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        Page page = roleService.getRolePage(pageNo,pageSize);
        Result result = new Result();
        result.setData(page);
        return result;

    }

    @RequestMapping(value = "/getRoleList")
    public Result getRoleList(){
        List<Role> roleList = roleService.getRoleList();
        Result result = new Result();
        result.setData(roleList);
        return result;
    }

    @RequestMapping(value = "/getRoleByRoleId")
    public Result getRoleByRoleId(Integer roleId){
        List<Role> roleByRoleId = roleService.getRoleByRoleId(roleId);
        Result result = new Result();
        result.setData(roleByRoleId);
        return result;
    }

}
